/**
 * 
 */
package org.crystal.jqweb.config;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
/**
 * @author Crystal
 *
 */
public class ConfigFile {
	
	private  final  Log log = LogFactory.getLog(getClass());
	private static ConfigFile instance = null;
	private String webRoot;
	
	
	public static synchronized ConfigFile getInstance() {
		if (instance == null) {
			instance = new ConfigFile();
			instance.log.debug("getInstance()" +" new ConfigFile instance");
		}
		return instance;
	}
	/**
	 * 
	 */
	public ConfigFile() {
		super();
		// TODO Auto-generated constructor stub
		String webRoot = ConfigFile.class.getResource("ConfigFile.properties").getFile();
		String fileProtocol=ConfigFile.class.getResource("ConfigFile.properties").getProtocol();
		LogFactory.getLog(ConfigFile.class).info("getWebRoot getProtocol:"+ConfigFile.class.getResource("ConfigFile.properties").getProtocol());
		System.out.println("getfile:"+webRoot);
		if(fileProtocol.equalsIgnoreCase("file")){
			//   /D:/SoftWare/apache-tomcat-5.5.30/webapps/JqWeb
			if(webRoot.indexOf("file:")==0){
				webRoot=webRoot.substring(5, webRoot.length());
			}
		}else if(fileProtocol.equalsIgnoreCase("jar")){
			//   file:/D:/SoftWare/apache-tomcat-5.5.30/webapps/JqWeb
			if(webRoot.indexOf("file:")==0){
				webRoot=webRoot.substring(5, webRoot.length());
			}
		}else if(fileProtocol.equalsIgnoreCase("wsjar")){
			if(webRoot.indexOf("file:")==0){
				webRoot=webRoot.substring(5, webRoot.length());
			}			
		}else if(webRoot.equalsIgnoreCase("file:")){
			webRoot=webRoot.substring(5, webRoot.length());
		}
		
		///   /WEB-INF/
		if(webRoot.indexOf("/WEB-INF/")!=-1){
			webRoot=webRoot.substring(0, webRoot.indexOf("/WEB-INF/"));
		}
		
		LogFactory.getLog(ConfigFile.class).info("getWebRoot() Set web app root system property: 'jqwebapp.path':"+webRoot);
		System.setProperty("jqwebapp.path", webRoot);
		this.webRoot= webRoot;
		
	}
	
	public  String getWebRoot(){
		return webRoot;
	}

}
